home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: Overload of operator=
- Date: 17 Jan 1996 19:08:05 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Jan17200805@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <30FBCAA1.34A7@novell.com>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: Greg Johnson's message of Tue, 16 Jan 1996 15:56:17 +0000
-
- In article <30FBCAA1.34A7@novell.com> Greg Johnson <gregjo@novell.com> writes:
-
- I want to overload the assignment operator, for some debugging purposes.
- But after I overload the operator, somehow, I need to perform the
- default assignment. How do I do that if I don't know the exact size
- of the source or destination?
- How do I avoid recursion and perform the default behavior?
- For example:
-
- class BaseObj {
- public:
- long x;
- BaseObj& operator=(BaseObj&);
- BaseObj(void) : x(0) {};
- };
-
- BaseObj& BaseObj::operator=(BaseObj & ptr)
- {
- *this = ptr; // this will recursively call operator=
- return *this;
- }
-
- void foo()
- {
- BaseObj bo1;
- BaseObj bo2;
-
- bo1.x = 99;
- bo2 = bo1;
- }
-
- in operator=, the '*this = ptr' line causes a recursive call.
- How can I avoid the recursion and perform the correct assignment?
- I could do a memcpy, but I need to determine the size of the source
- and destination (either one may be base pointers to a more complex
- class.)
- Is there a way to determine the size of an object created with 'new'?
-
- Just drop the definition of assignment operator in the non-debugging
- code. The default assignment-op does exactly what you are looking for.
-
- Enno
-